//全局变量
var mydata2=[];




// 将从页面1中跳转过来的用户id参数进行保存

function getUrlParams(name) { 
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); //定义正则表达式 
    var r = window.location.search.substr(1).match(reg);  
    if (r != null) return unescape(r[2]); 
  return null; 
}
var param=getUrlParams('id');
console.log(param);
//将d2数据进行渲染
StuOrder(param);

//关闭当前页面
function back(){
	window.close();
}
//跳转到产品页面param
function href(i){
		window.open(`./3.html?id=${param}`);	
}

//订单详情事件
function StuOrder(i) {
	
	$.ajax({
		type: "post", //post put get 等等
		url: "http://img.zhangjiankeji.cn/StuWeb/ashx/DStuCusItem.ashx",
		data: { //要传入ashx文件的数据	
			"OutID": i,
		},
		success: function(data, textStatus) {
			//判断返回错误情况
			console.log(JSON.parse(data));
			mydata2 = JSON.parse(data);
			if(mydata2){
				table_2();
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert('返回错误');
			alert(errorThrown);
		}
	});


}

//此方法负责加载表格数据-表格第二个!!!
function table_2() {
	var str = '';
	str =
		`<h4 style="padding: 10px;" class="table_2_h4"><span onclick="back()" class="glyphicon glyphicon-chevron-left" style="color: rgb(52, 52, 52);"></span>详情页</h4>
		
		`

	for (var i = 0; i < mydata2.length; i++) {
		str +=
			`
				<ul class="nav nav_body2">	
					<li>
						<span></span>
						<span>${i + 1}</span>
					</li>
					<li>
						<span></span>
						<span>							
								<input type="checkbox" data-id="${i}">
						</span>
					</li>
					<li>
						<span></span>
						<span> <img src="DStuffXHImg/${mydata2[i].DMode}.jpg"/></span>
					</li>
					<li>
						<ul class="nav nav_body3">
							<li>
								<span></span>
								<span>${mydata2[i].DName}</span>
							</li>
							<li>
							<span  class="label label-default">型号:</span>
								<span>${mydata2[i].DMode}</span>
							</li>
							<li>
								<span  class="label label-info">规格:</span>
								<span>${mydata2[i].DGge}</span>
							</li>
							<li>
								<span  class="label label-info">颜色:</span>
								<span>${mydata2[i].ColorName}</span>
							</li>
							<li>
								<span  class="label label-info">单价:</span>
								<span>${mydata2[i].DPrice}</span>
							</li>
							<li>
								<span   class="label label-info">数量:</span>
								<span><input onchange="changnum(event,${i})" style="width:50px;" class="dnums" type="number" value="${mydata2[i].DNums}"></span>	
							</li>
							<li>
								<span  class="label label-info">金额:</span>
								<span>${mydata2[i].SumPrice}</span>
							</li>
							<li>
								<span  class="label label-info">重:</span>
								<span>${mydata2[i].Weight}</span>
							</li>

						</ul>
						
					</li>
					
	
					<li>
						<ul class="nav nav_body3_2">
							<li >
								<span  class="label label-info">米重:</span>
								<span>${mydata2[i].DWght}</span>
							</li>
							<li>
								<span  class="label label-info">壁厚:</span>
								<span>${mydata2[i].BiHou}</span>
							</li>
							<li>
								<span class="label label-info">注释:</span>
								<span><input style="width:100px;" class="remark" type="text" value="${mydata2[i].Remark}"></span>
							</li>
						</ul>
					</li>	
				
			
					
				</ul>
			`
	}
	str=`<div>${str}</div>	
		<div class="buttom">
			<button type="button" class="btn btn-info" onclick='update()'>选择修改</button><button type="button" class="btn btn-danger" onclick='del()'>选择删除</button>
		</div>`
	d2.innerHTML = str;
}
//修改选中的d2表格
function update(){
	
	//遍历选中的数组
	$(".nav_body2 input:checked").each(function() {
		console.log($(this).attr("data-id"));
		var i = $(this).attr("data-id");
		//对选中的元素的数量和备注进行修改
		 mydata2[Number(i)].DNums=$(this).parents(".nav_body2").find('.dnums').val();
		 mydata2[Number(i)].Remark=$(this).parents(".nav_body2").find('.remark').val();
	})
	//重新渲染页面
	table_2();
}

//删除选中的d2表格
function del(){
	if (window.confirm("您确定要删除么？")) {
		
		//遍历选中的数组
		$(".nav_body2 input:checked").each(function() {
			console.log($(this).attr("data-id"));
			var i = $(this).attr("data-id")
			//对选中的元素进行删除
			var a = mydata2.splice(typeof Number(i), 1);
		})
		//重新渲染页面
		table_2();
	
	} else {
		return;
	}
}

//数量改变引起重量,金额的改变
function changnum(e,i){
	console.log(e)
	mydata2[i].DNums=$(e.target).val();
		console.log(i)
	// 重量=长度/1000* 米重*数量
	mydata2[i].Weight=mydata2[i].DLong/1000*mydata2[i].DNums*mydata2[i].DWght;
	if(mydata2[i].DWght){
		// 金额=单价* 重量
	mydata2[i].SumPrice=mydata2[i].DPrice*mydata2[i].Weight;
	}else{
		// 没有米重的情况下。金额=数量*单价
	mydata2[i].SumPrice=mydata2[i].DPrice*mydata2[i].DNums;
	}
	//重新渲染页面
	table_2();
}
